package entites;

import java.util.HashMap;
import java.util.Map;

import utils.LANPackage;
import utils.PackageType;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;

public class Source extends Sim_entity {

	private Sim_negexp_obj delay;
	private Sim_port toSpooler;

	public Source(String name, double mean) {
		super(name);

		// Configuração da distribuição
		this.delay = new Sim_negexp_obj(name, mean);
		this.add_generator(this.delay);

		// Configuração das portas
		this.toSpooler = new Sim_port("toSpooler");
		this.add_port(this.toSpooler);
	}

	@Override
	public void body() {
		this.sim_pause(10.0);
		while (Sim_system.running()) {

			double init_time = Sim_system.sim_clock();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("INIT", init_time);

			// Construção do pacote
			Boolean priority = this.get_name().equals("priority_source");
			LANPackage lanPackage = new LANPackage(priority, map);
			
			this.sim_schedule(this.toSpooler, 0.0,
					PackageType.REQUEST.ordinal(), lanPackage);
			this.sim_pause(this.delay.sample());
		}
	}
}
